package me.send.data.service.provider;

import java.util.Map;

import me.send.business.base.Pager;
import me.send.business.base.SendVariable;
import me.send.data.service.pojo.Orders;
import me.send.framework.util.LoggerUtils;

public class OrdersProvider {


	public String list_page(Map<String, Object> parameter) {
		// 获取 对象 查询对象 2015年6月23日17:34:01
		Orders orders = (Orders) parameter.get(SendVariable.DATA);
		// 判断下面的字段 是否是null
		String sql = "select * from orders where 1=1 ";
		if (orders != null) {
			if (orders.getDescption() != null
					&& !orders.getDescption().equals("")) {
				sql += " and descption like CONCAT('%',#{DATA.descption},'%' ) ";
			}
			if (orders.getOrdertypeid() > 0) {
				sql += " and ordertypeid = #{DATA.ordertypeid} ";
			}
			if (orders.getStatus() > 0) {
				sql += " and status = #{DATA.status}";
			}
		}
		sql += " limit #{PAGE.start},#{PAGE.rows} ";
		return sql;
	}
	
	public String count(Map<String, Object> parameter){
		// 获取 对象 查询对象 2015年6月23日17:34:01
		Orders orders = (Orders) parameter.get(SendVariable.DATA);
		// 判断下面的字段 是否是null
		String sql = "select count(1) as total from orders where 1=1 ";
		if (orders != null) {
			if (orders.getDescption() != null
					&& !orders.getDescption().equals("")) {
				sql += " and descption like CONCAT('%',#{DATA.descption},'%' ) ";
			}
			if (orders.getOrdertypeid() > 0) {
				sql += " and ordertypeid = #{DATA.ordertypeid} ";
			}
			if (orders.getStatus() > 0) {
				sql += " and status = #{DATA.status}";
			}
		}
		return sql;
	}
	
	public String ordersCount(Map<String, Object> orders){
		// 判断下面的字段 是否是null
		StringBuffer sql = new StringBuffer("select count(1) as total from orders where 1=1 ");
		if (orders != null) {
			if (orders.get("keyCode") != null && !"".equals(orders.get("keyCode"))) {
				sql.append(" and descption like '%'"+orders.get("keycode")+"'%' ");
			}
			if (orders.get("otype")!=null&&(Integer)orders.get("otype") > 0) {
				sql.append(" and ordertypeid ="+(Integer)orders.get("otype"));
			}
			if (orders.get("ostatus")!=null&&(Integer)orders.get("ostatus") > 0) {
				sql.append( " and status = "+(Integer)orders.get("ostatus"));
			}
		}
		return sql.toString();
	}
	
	public String getOrders(Map<String, Object> orders){
		// 判断下面的字段 是否是null
		StringBuffer sql = new StringBuffer("select o.ordertypeid,o.status,o.descption,o.tip,o.orderid,u.nickname as total from orders o");
		sql.append(" LEFT JOIN `user` u ON u.userid = o.userid where 1=1 ");
		if (orders != null) {
			if (orders.get("keyCode") != null && !"".equals(orders.get("keyCode"))) {
				sql.append(" and descption like '%'"+orders.get("keycode")+"'%' ");
			}
			if (orders.get("otype")!=null&&(Integer)orders.get("otype") > 0) {
				sql.append(" and ordertypeid ="+(Integer)orders.get("otype"));
			}
			if (orders.get("ostatus")!=null&&(Integer)orders.get("ostatus") > 0) {
				sql.append(" and o.`status` = "+(Integer)orders.get("ostatus"));
			}
			if ((Pager)orders.get("page")!=null) {
				Pager pager = (Pager)orders.get("page");
				LoggerUtils.info(getClass().getName(), "start:"+pager.getStart()+",page:"+pager.getPage()+",rows:"+pager.getRows());
				sql.append(" LIMIT ").append(pager.getStart()).append(",").append(pager.getRows());
			}
		}
		return sql.toString();
	}

}
